const WebSocket = require('ws')
const Wxsuser = require('../../models/Wxsuser')

module.exports = (server) => {
    const ws = new WebSocket.Server({ server, path: "/ws" })
    ws.on("connection", function (socket, req) {
        const ip = req.headers['x-real-ip'].split(/\s*,\s*/)[0];
        socket.on("message", async function (msg) {
            msg = JSON.parse(msg)
            const user = await Wxsuser.findOne({openid: msg.openid})
            let obj = {
                touxiang: user.touxiang,
                nicheng: user.username,
                message: msg.message,
                openid: user.openid
            }
            obj = JSON.stringify(obj)
            ws.clients.forEach(function each(client) {
                client.send(obj);
            });
        })

        // 获取在线用户数量
    })
}